package com.haoran.dwz.common.http;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.haoran.dwz.common.util.Md5;

public class KyeHttpClientRequest extends HttpClientRequest {
	private static Logger logger = LoggerFactory.getLogger(KyeHttpClientRequest.class);

	private String accessAdds;

	private String accessCode;

	private String accessKey;

	private KyeHttpClientRequest(String accessAdds, String accessCode, String accessKey) {
		this.accessAdds = accessAdds;
		this.accessCode = accessCode;
		this.accessKey = accessKey;
	}

	private static final String EMPTY_JSON = "{}";

	public KyeHttpClientRequest build(String api) {
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("kye", accessCode);
		headers.put("access-token", generate(Collections.<String, Object>emptyMap()));
		headers.put("Content-Type", "application/json;charset=UTF-8");
		setHeaders(headers);
		setUrl(accessAdds.concat(api));
		setBody(EMPTY_JSON);
		return this;
	}

	public static KyeHttpClientRequest build(String accessAdds, String accessCode, String accessKey) {
		return new KyeHttpClientRequest(accessAdds, accessCode, accessKey);
	}

	public KyeHttpClientRequest build(String api, Map<String, Object> paramMap) {
		logger.info("请求API: {} ", api);
		logger.info("入参JSON: {} ", JSON.toJSONString(paramMap, true));
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("kye", accessCode);
		headers.put("access-token", generate(paramMap));
		headers.put("Content-Type", "application/json;charset=UTF-8");
		setHeaders(headers);
		setUrl(accessAdds.concat(api));
		String body = JSON.toJSONString(paramMap);
		;
		setBody(body);
		return this;
	}

	public KyeHttpClientRequest build(String api, String json) {
		logger.info("请求API: {} ", api);
		logger.info("入参JSON: {} ", json);
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("kye", accessCode);
		@SuppressWarnings("unchecked")
		Map<String, Object> paramMap = com.alibaba.fastjson.JSONObject.parseObject(json, Map.class);
		headers.put("access-token", generate(paramMap));
		headers.put("Content-Type", "application/json;charset=UTF-8");
		setHeaders(headers);
		setUrl(accessAdds.concat(api));
		String body = json;
		setBody(body);
		return this;
	}

	private String generate(Map<String, Object> paramMap) {
		StringBuffer sb = new StringBuffer();
		List<String> keyList = new ArrayList<String>();
		for (String key : paramMap.keySet()) {
			if (!(paramMap.get(key) == null || paramMap.get(key).toString().trim().equals("")
					|| "null".equalsIgnoreCase(paramMap.get(key).toString()))) {
				keyList.add(key);
			}
		}
		Collections.sort(keyList);
		for (int i = 0; i < keyList.size(); i++) {
			sb.append(keyList.get(i));
			sb.append(paramMap.get(keyList.get(i)));
		}
		String str = accessKey.concat(sb.toString());
		logger.info("Sorted the params: {}", str);
		String access_token = Md5.getVal_utf(str).toUpperCase();
		logger.info("MD5:{}", access_token);
		return access_token;
	}

	public String getAccessAdds() {
		return accessAdds;
	}

	public void setAccessAdds(String accessAdds) {
		this.accessAdds = accessAdds;
	}

	public String getAccessCode() {
		return accessCode;
	}

	public void setAccessCode(String accessCode) {
		this.accessCode = accessCode;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}
}

